// import { good } from '../models/index'
const { good, shop, translation, kind } = require('../models/index')
const _ = require('lodash')
const errorHelper = require('../util/errorHelper')

class GoodController {
  // constructor() {}

  async createGoods(goods) {
    const result = await good.bulkCreate(goods)
    if (!result.length) {
      throw errorHelper(400, 'post data error')
    }
    return result
  }

  async upsertGood(goodData) {
    const result = await good.upsert(goodData)
    return result
  }

  async delete(goods) {
    const promiseArr = []
    _.forEach(goods, (item) => {
      promiseArr.push(good.update({ disable: true }, { where: item }))
    })
    return Promise.all(promiseArr).then((data) => {
      return true
    })
  }

  static async findByKind(kindId) {
    await good.findAll({ where: { kindId } })
  }

  async getGoods() {
    const goods = await good.findAll({})
    if (!goods.length) {
      throw errorHelper(400, 'field error')
    }
    return goods
  }

  async count() {
    const count = await good.count()
    return count
  }
}
module.exports = new GoodController()
